#include "foldercopierthread.h"
#include "QFile"
#include "logger.h"

FolderCopierThread::FolderCopierThread(const QString letter,const QString source,const QString to)
{
    this->driveLetter = QString(letter);
    this->pathSource = QString(source);
    this->pathTo = QString(to);

    QObject::connect(&_copier, SIGNAL(finishCopy()),
            this, SLOT(finished()));
    QObject::connect(&_copier, SIGNAL(stopedCopy()),
            this, SLOT(canceled()));

}

FolderCopierThread::FolderCopierThread(const char *letter,const char *source,const char *to)
{
    this->driveLetter = QString(letter);
    this->pathSource = QString(source);
    this->pathTo = QString(to);

    QObject::connect(&_copier, SIGNAL(finishCopy()),
            this, SLOT(finished()));

    QObject::connect(&_copier, SIGNAL(stopedCopy()),
            this, SLOT(canceled()));

}
FolderCopierThread::~FolderCopierThread()
{
    this->wait();
    if(_file!=NULL)
    {
        if(_file->isOpen())
            _file->close();
        delete _file;

    }
}

QString FolderCopierThread::getValidFolderName()
{
    QByteArray folderName(this->titleId.toAscii());
    QByteArray newFolderName;
    for(int i=0; i<folderName.count();i++)
    {
        if(
                folderName.at(i)=='/'||
                folderName.at(i)=='\\'||
                folderName.at(i)==':'||
                folderName.at(i)=='*'||
                folderName.at(i)=='<'||
                folderName.at(i)=='>'||
                folderName.at(i)=='?'||
                folderName.at(i)=='"'
                )
        {
            newFolderName.append('_');
        }
        else
        {
            newFolderName.append(folderName.at(i));
        }
    }
    return newFolderName;
}

void FolderCopierThread::run()
{
    _copier.myLog = &(Logger::log());
    _copier.setDriveLetter(this->driveLetter);
    _copier.setPathSource(this->pathSource);
    QString path = this->pathTo +"\\"+ this->getValidFolderName();
    _copier.setPathDest(path);
    _copier.startCopy();
    exit();
}
void FolderCopierThread::setId(QString id)
{
    _id = id;
}

QString FolderCopierThread::getId()
{
    return _id;
}
bool FolderCopierThread::getParentalControl()
{
    return parentalControl;
}
void FolderCopierThread::setParentalControl(bool value)
{
    this->parentalControl = value;
}
void FolderCopierThread::setDriveLetter(QString value)
{
    this->driveLetter = value;
}
void FolderCopierThread::setDiscId(QString discId)
{
    this->_discId = discId;
}
void FolderCopierThread::setPathTo(QString path)
{
    pathTo = path;
}

QString FolderCopierThread::getPathTo()
{
    return pathTo;
}
QString FolderCopierThread::getDiscId()
{
    return _discId;
}

void FolderCopierThread::startCopy(const QString &titleId,const bool parentalControl)
{
    this->titleId=titleId;
    this->setParentalControl(parentalControl);
    this->start();
}

void FolderCopierThread::stopCopy()
{
    qDebug() << "void FolderCopierThread::stopCopy()";
    if(this->isRunning())
    {
        this->_copier.stopCopy();
    }
    else
    {
        canceled();
    }
}

void FolderCopierThread::finished()
{
    qDebug() << "FolderCopierThread::finished()";

    emit copyFinished(this->getId(),_copier.getPathDest(),this->getDiscId(),this->getParentalControl());
}

void FolderCopierThread::canceled()
{
    qDebug() << "FolderCopierThread::canceled()";

    emit copyCanceled(this->getId(),_copier.getPathDest(),this->getDiscId(),this->getParentalControl());
}

void FolderCopierThread::setTitleId(QString titleId)
{
    this->titleId = titleId;
}
int FolderCopierThread::checkProgress()
{
    qDebug() << "FolderCopierThread::checkProgress()";

    return _copier.getProgress();;
}
QString FolderCopierThread::getDriveLetter()
{
    return this->driveLetter;
}
